package handler

import (

	"golang.org/x/net/context"
	account "git.oschina.net/fanbuchi/xgggh/oauth2/proto/account"
	"git.oschina.net/fanbuchi/xgggh/oauth2/model"
	"git.oschina.net/fanbuchi/xgggh/oauth2/db"
	"encoding/json"
	"git.oschina.net/fanbuchi/xgggh/oauth2/service"

)

type Account struct{}



//micro query com.xgggh.srv.oauth2 Account.Create '{"userName":"zhangsan","password":"qw123456","roleId": 1,"metaUserId":"110"}'
//micro query com.xgggh.srv.oauth2 Account.Create '{"userName":"15108280907","password":"qw123456","roleId": 1,"metaUserId":"110"}'
//micro query com.xgggh.srv.oauth2 Account.Create '{"userName":"lisi","password":"qw123456","roleId": 1,"metaUserId":"110"}'

// Call is a single request handler called via client.Call or the generated client code
func (e *Account) Create(ctx context.Context, req *account.CreateReq, rsp *account.CreateRsp) error {
	result, err:=service.AccountCreate(ctx,req)
	if result!=nil {
		rsp.Data=&account.AccountMsg{Id:uint64(result.ID),
			UserName:result.UserName}
	}
	return err
}

//micro query com.xgggh.srv.oauth2 Account.Query '{"id":247279268683055105}'
//micro query com.xgggh.srv.oauth2 Account.Query {\"id\":359114214592184321}
func (e *Account) Query(ctx context.Context, req *account.QueryReq, rsp *account.QueryRsp) error {
	var _account model.Account;
	err:=db.GetDB().First(&_account, req.Id).Error
	if _account.ID<=0 {
		return nil
	}

	var deleted int64
	if _account.DeletedAt!=nil {
		deleted=_account.DeletedAt.Unix()
	}

	rsp.Data=&account.AccountMsg{Id:uint64(_account.ID),
		UserName:_account.UserName,
		RoleId:_account.RoleId,
		CreatedAt:_account.CreatedAt.Unix(),
		UpdatedAt:_account.UpdatedAt.Unix(),
		DeletedAt:deleted,
		}
	return err
}

//micro query com.xgggh.srv.oauth2 AccountSrv.Update '{"id":245064531848396801,"account":{"userName":"lisi","roleId":120,"password":"123456"}}'

func (e *Account) Update(ctx context.Context, req *account.UpdateReq, rsp *account.UpdateRsp) error {
	_account:=req.Account
	if  _account==nil{
		return nil
	}
	_account.Id=uint64(req.Id)
	err:= db.GetDB().Model(&_account).Updates(model.Account{
		Password:_account.Password,
		MetaAccountId:_account.MetaUserId,
		UserName:_account.UserName,
		RoleId:_account.RoleId}).Error
	if err ==nil{
		rsp.Data=_account
	}
	return err
}
//micro query com.xgggh.srv.oauth2 AccountSrv.Delete '{"id":245064531848396801}'

func (e *Account) Delete(ctx context.Context, req *account.DeleteReq, rsp *account.DeleteRsp) error {
	_account:=model.Account{}
	_account.ID=uint(req.Id)
	err:=db.GetDB().Delete(&_account).Error
	return err
}

//micro query com.xgggh.srv.oauth2 AccountSrv.Search '{"page":1,"per":10}'
//search by userName,metaUserId ...
//micro query com.xgggh.srv.oauth2 AccountSrv.Search '{"page":1,"per":10,"params":{"userName":"15108280907"}}'
func (e *Account) Search(ctx context.Context, req *account.SearchReq, rsp *account.SearchRsp) error {
	accounts,err:=service.AccountSearch(req)
	if err!=nil {
		return err
	}

	var result [] *account.AccountMsg
	data,err:=json.Marshal(accounts)
	json.Unmarshal(data,&result)
	if err ==nil{
		rsp.Data=result
	}
	return  err
}